﻿using FreeRobot.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FreeRobot.View
{
    public partial class FrmMain : Form
    {
        readonly RobotHelper _robot;
        public FrmMain()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            _robot = new RobotHelper();
            _robot.CompletedCallback += _robot_CompletedCallback;
            _robot.SetMap(this.robotMap1);
            _robot.SetMap(0);
        }

        private void _robot_CompletedCallback()
        {
            MessageBox.Show("抵达终点");
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            string code= @"
using System;

namespace FreeRobot
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static void Main()
        {
                      
        }
    }
}
            ";
            this.rtxtCode.Text = code;
            _robot.Reset();
            _robot.SetMap(0);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string txt = this.rtxtCode.Text;
            if (string.IsNullOrWhiteSpace(txt)) return;
            Task.Factory.StartNew(() =>
            {
                _robot.Excute(txt);
            });
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            _robot.Dispose();
        }

        private void btnCreateMap_Click(object sender, EventArgs e)
        {
            _robot.SetMap(1);
        }
    }
}
